<?php

namespace Yiru\LaravelComposer\Helpers;

use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Auth;
use Log;
use DB;
use Exception;

use Symfony\Component\Console\Input\InputOption;
use mikehaertl\shellcommand\Command as Shell;
class Npm 
{
   
    public static function  shell($shell,$output,$msg){
       
        if($shell->execute()){
            $ret=$shell->getOutput();            
            if(Str::of($ret)->length()==0) return null;            
            $output->note($shell->getExecCommand().PHP_EOL.$ret);  
            return $ret;
        }else{
            $ret=$shell->getError();                 
            $output->error($shell->getExecCommand().PHP_EOL.$ret);  
            $exitCode = $shell->getExitCode();
            if(in_array($msg,['NPM_LIST',"NPM_UNINSTALL"])) return false;
            throw new Exception($msg);
        }    
    }
   
    /// 使用env配置的报名
    public static function getName($pkg){
        if(empty($pkg)){
            throw new Exception('必须提供参数');
        }        
        if(Str::startsWith($pkg,'@')){            
              $npmName='@'.String($pkg)->segment('@',1);
        }else $npmName=String($pkg)->segment('@',0).'';
        
        $name=config('npm.pkgs.'.$npmName.'.name');
        if(Str::of($name)->isNotEmpty()){
              return $name;
        }else return $npmName;  
    }

    public static function getVer($pkg){
        if(empty($pkg)){
            throw new Exception('必须提供参数');
        }
        if(Str::startsWith($pkg,'@')){            
              $npmVer=String($pkg)->segment('@',2);
        }else $npmVer=String($pkg)->segment('@',1);        
                
        if(Str::of($npmVer)->isEmpty()){                        
              return config('npm.pkgs.'.$pkg.'.ver');            
        }else return $npmVer.'';
    }

    public static function getPkg($pkg){
        if(empty($pkg)){
            throw new Exception('必须提供参数');
        }
        $name=self::getName($pkg);
        $ver=self::getVer($pkg);        
        if(Str::of($ver)->isEmpty()) return $name;
        return $name.'@'.$ver;        
    }
    // $npm_dir=Str::of($npm_dir)->replaceMatches('/[@]/', function ($match) {
            //                                     return '\\'.$match[0];
            //                                    }).'';
    public static function getDistDir($pkg){
        if(empty($pkg)){
            throw new Exception('必须提供参数');
        }
        $npmName=Npm::getName($pkg);
        $npm_dirs=config('npm.pkgs.'.$npmName.'.dist',['dist']);
        foreach ($npm_dirs as $key=>$npm_dir) {
            $npm_dirs[$key]=Str::of($npm_dir)->prepend(base_path('node_modules/'.$npmName.'/')).'';
        }       
        return $npm_dirs; 
    }      
     /// 使用env配置的报名
    public static function envName($pkg){
        if(empty($pkg)){
            throw new Exception('必须提供参数');
        }        
        if(Str::startsWith($pkg,'@')){            
              $npmName='@'.String($pkg)->segment('@',1);
        }else $npmName=String($pkg)->segment('@',0).'';
        
        return $npmName;  
    }

     public static function getEnvVer($pkg){
        if(empty($pkg)){
            throw new Exception('必须提供参数');
        }
        if(Str::startsWith($pkg,'@')){            
              $npmVer=String($pkg)->segment('@',2);
        }else $npmVer=String($pkg)->segment('@',1);        
                
        if(Str::of($npmVer)->isEmpty()){                        
              return config('npm.pkgs.'.$pkg.'.ver');            
        }else return $npmVer.'';
    }

    public static function getEnvDir($pkg){        
        if(empty($pkg)){
            throw new Exception('必须提供参数');
        }
        $envName=Npm::envName($pkg);
        $npmName=Npm::getName($pkg);
        $npmVer =Npm::getVer($pkg);
        $root_dir=config('npm.npm.root_dir',public_path('/libs/'));
        if(Str::of($npmVer)->isEmpty())return $root_dir.'/'.$npmName.'/';
        else         return $root_dir.'/'.$npmName.'/'.$npmVer.'/';        
    }
    
    public static function getEnvUrl($pkg){
        if(empty($pkg)){
            throw new Exception('必须提供参数');
        }
        $name=String($pkg)->segment('@',0);
        $ver=String($pkg)->segment('@',1);
        $root=config('npm.npm.root_url','/libs/');
        if(Str::of($ver)->isEmpty())return $root.'/'.$name.'/';
        else         return $root.'/'.$name.'/'.$ver.'/';
    }

}